Skip to content

lib: short-circuit WebIDL BufferSource SAB check#62833

Open
panva wants to merge 1 commit intonodejs:mainfrom
panva:fast-buffersource
Open

lib: short-circuit WebIDL BufferSource SAB check#62833
panva wants to merge 1 commit intonodejs:mainfrom
panva:fast-buffersource

Conversation

@panva
Copy link
Copy Markdown
Member

@panva panva commented Apr 19, 2026

Avoid the C++ isSharedArrayBuffer binding call on the BufferSource and ArrayBufferView converter hot path by brand-checking the backing buffer via the ArrayBuffer.prototype.byteLength getter: the getter succeeds on every real ArrayBuffer (the common case) and throws only on SharedArrayBuffers, mirroring the brand check V8 performs internally. This preserves the existing SharedArrayBuffer rejection semantics independent of the receiver's prototype chain, including pathological cases such as a SharedArrayBuffer whose prototype has been reassigned to ArrayBuffer.prototype.


Local:

                                                    before ops/sec    after ops/sec    speedup
input="arraybuffer"                                     16,889,535       16,892,386      equal
input="buffer"                                          14,935,801      109,679,440      7.34x
input="dataview"                                        16,317,802      175,225,973     10.74x
input="uint8array"                                      15,414,470      112,565,076      7.30x
input="uint8clampedarray"                               14,872,961      109,705,509      7.38x
input="int8array"                                       15,157,350      107,936,966      7.12x
input="uint16array"                                     15,090,143      110,854,507      7.35x
input="int16array"                                      14,903,084      111,035,797      7.45x
input="uint32array"                                     14,646,015      111,284,993      7.60x
input="int32array"                                      14,516,087      110,004,950      7.58x
input="float16array"                                    15,233,858      109,795,595      7.21x
input="float32array"                                    15,345,072      111,418,806      7.26x
input="float64array"                                    14,764,074      110,252,063      7.47x
input="bigint64array"                                   15,217,740      108,292,498      7.12x
input="biguint64array"                                  15,027,624      110,549,409      7.36x

CI Benchmark: #62833 (comment)

Bench pre a136367
                                                    before ops/sec    after ops/sec    speedup
input="arraybuffer"                                     16,889,535       16,890,841      equal
input="buffer"                                          14,935,801      125,791,768      8.42x
input="dataview"                                        16,317,802      177,117,590     10.85x
input="uint8array"                                      15,414,470      125,996,487      8.17x
input="uint8clampedarray"                               14,872,961      124,526,604      8.37x
input="int8array"                                       15,157,350      124,766,713      8.23x
input="uint16array"                                     15,090,143      123,827,379      8.21x
input="int16array"                                      14,903,084      120,681,548      8.10x
input="uint32array"                                     14,646,015      120,610,045      8.23x
input="int32array"                                      14,516,087      120,635,143      8.31x
input="float16array"                                    15,233,858      120,150,187      7.89x
input="float32array"                                    15,345,072      124,486,881      8.11x
input="float64array"                                    14,764,074      122,339,186      8.29x
input="bigint64array"                                   15,217,740      125,704,732      8.26x
input="biguint64array"                                  15,027,624      125,533,190      8.35x

@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

nodejs-github-bot commented Apr 19, 2026

Review requested:

  • @nodejs/performance
  • @nodejs/web-standards

@nodejs-github-bot nodejs-github-bot added the needs-ci PRs that need a full CI run. label Apr 19, 2026
@panva panva added the web-standards Issues and PRs related to Web APIs label Apr 19, 2026
Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@panva panva added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. request-ci Add this label to start a Jenkins CI on a PR. labels Apr 19, 2026
Comment thread lib/internal/webidl.js Outdated
@panva panva removed request-ci Add this label to start a Jenkins CI on a PR. author ready PRs that have at least one approval, no pending requests for changes, and a CI started. labels Apr 19, 2026
Comment thread lib/internal/webidl.js Outdated
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 19, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.63%. Comparing base (a79e224) to head (7e3be29).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #62833      +/-   ##
==========================================
- Coverage   89.64%   89.63%   -0.01%     
==========================================
  Files         706      706              
  Lines      219024   219150     +126     
  Branches    41960    41984      +24     
==========================================
+ Hits       196338   196438     +100     
- Misses      14587    14606      +19     
- Partials     8099     8106       +7     
Files with missing lines Coverage Δ
lib/internal/webidl.js 96.73% <100.00%> (+0.10%) ⬆️

... and 37 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Signed-off-by: Filip Skokan <panva.ip@gmail.com>
@panva panva force-pushed the fast-buffersource branch from a136367 to 7e3be29 Compare April 20, 2026 08:15
Comment thread lib/internal/webidl.js
@panva
Copy link
Copy Markdown
Member Author

panva commented Apr 20, 2026

Why am I only seeing this in https://ci.nodejs.org/view/Node.js%20benchmark/job/benchmark-node-micro-benchmarks/1829/ 🤔

10:28:52                                                                  confidence improvement accuracy (*)   (**)   (***)
10:28:52 misc/webidl-buffer-source.js n=1000000 input='arraybuffer'                       3.80 %       ±7.21% ±9.62% ±12.56%
10:28:52 misc/webidl-buffer-source.js n=1000000 input='bigint64array'                     2.41 %       ±4.49% ±5.98%  ±7.80%
10:28:52 misc/webidl-buffer-source.js n=1000000 input='biguint64array'           ***      8.00 %       ±4.21% ±5.60%  ±7.29%
10:28:52 misc/webidl-buffer-source.js n=1000000 input='buffer'                   ***      8.05 %       ±3.87% ±5.15%  ±6.72%
10:28:52 misc/webidl-buffer-source.js n=1000000 input='dataview'                 ***     11.65 %       ±6.27% ±8.36% ±10.91%
10:28:52 misc/webidl-buffer-source.js n=1000000 input='float16array'                      4.54 %       ±5.32% ±7.09%  ±9.25%
10:28:52 misc/webidl-buffer-source.js n=1000000 input='float32array'              **      7.02 %       ±4.46% ±5.94%  ±7.76%
10:28:52 misc/webidl-buffer-source.js n=1000000 input='float64array'             ***      8.54 %       ±4.58% ±6.09%  ±7.93%
10:28:52 misc/webidl-buffer-source.js n=1000000 input='int16array'                **      6.07 %       ±4.48% ±5.96%  ±7.77%
10:28:52 misc/webidl-buffer-source.js n=1000000 input='int32array'                **      6.74 %       ±4.79% ±6.37%  ±8.30%
10:28:52 misc/webidl-buffer-source.js n=1000000 input='int8array'                ***      8.97 %       ±4.45% ±5.93%  ±7.71%
10:28:52 misc/webidl-buffer-source.js n=1000000 input='uint16array'              ***      8.78 %       ±4.58% ±6.10%  ±7.94%
10:28:52 misc/webidl-buffer-source.js n=1000000 input='uint32array'              ***      9.52 %       ±4.48% ±5.97%  ±7.76%
10:28:52 misc/webidl-buffer-source.js n=1000000 input='uint8array'                 *      6.55 %       ±5.17% ±6.88%  ±8.96%
10:28:52 misc/webidl-buffer-source.js n=1000000 input='uint8clampedarray'        ***      8.37 %       ±3.98% ±5.31%  ±6.92%

It's still an improvement, but much less than what i'm looking at locally.

Comment thread lib/internal/webidl.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ci PRs that need a full CI run. web-standards Issues and PRs related to Web APIs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants